package com.core.framework.db.jdbc.support.dialect;

import org.apache.commons.lang3.StringUtils;

import com.core.framework.db.jdbc.page.Page;
import com.core.log.Log;
/**
 * MySql 数据库 SQL语句资料库
 * @author 刘涛
 * @since Jul 2, 2012
 */
public class Mysqldialect extends AbstractDialect {

	/**
	 * MYSQL分页语句
	 */
	public String getPageSQL( Page<?> page , String tableName, String whereContent) {
		if(StringUtils.isBlank(whereContent))
		{
			whereContent=" 1 = 1";
		}
		if(StringUtils.isNotBlank(page.getOrderby()))
		{
			whereContent = whereContent +" order by "+ page.getOrderby() +page.getDir() ;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT * FROM  ");
		sb.append(tableName);
		sb.append(" WHERE ");
		sb.append(whereContent);
		sb.append(" LIMIT ");
		sb.append((page.getPageNo()-1)*page.getPageSize());
		sb.append(",");
		sb.append((page.getPageNo())*page.getPageSize());
		Log.debug(sb);
		return sb.toString();
	}
	/**
	 * MySql 分页条数统计语句
	 */
	public String getPageCountSQL (String tableName ,String whereContent)
	{
		if(StringUtils.isBlank(whereContent))
		{
			whereContent=" 1 = 1";
		}
		StringBuffer sb = new StringBuffer();
		sb.append(" select count(1) from ");
		sb.append(tableName);
		sb.append(" where  ");
		sb.append(whereContent);
		Log.debug(sb);
		return sb.toString();
	}

}
